package edu.study.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import edu.study.api.common.dto.BaseRespDto;
import edu.study.api.common.dto.PageRespDto;
import edu.study.api.common.dto.RespDto;
import edu.study.common.util.cache.Lock;
import edu.study.common.util.cache.LockExecuteCallback;
import edu.study.user.api.DomainApi;
import edu.study.user.api.dto.DomainDto;
import edu.study.user.api.req.domain.AddDomainReq;
import edu.study.user.api.req.domain.DeleteDomainReq;
import edu.study.user.api.req.domain.ModifyDomainReq;
import edu.study.user.api.req.domain.ModifyDomainStatusReq;
import edu.study.user.api.req.domain.QueryDomainPageListReq;
import edu.study.user.api.req.domain.QueryDomainReq;
import edu.study.user.service.DomainService;

@RestController
public class DomainApiImpl implements DomainApi {

	@Autowired
    private Lock lock;

    @Autowired
    private DomainService domainService;

	@Override
	public BaseRespDto addDomain(@RequestBody AddDomainReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getDomainName(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = domainService.addDomain(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto deleteDomain(@RequestBody DeleteDomainReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getDomainNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = domainService.deleteDomain(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto modifyDomain(@RequestBody ModifyDomainReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getDomainNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = domainService.modifyDomain(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto modifyDomainStatus(@RequestBody ModifyDomainStatusReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getDomainNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = domainService.modifyDomainStatus(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public RespDto<DomainDto> queryDomain(@RequestBody QueryDomainReq req) {
		RespDto<DomainDto> resp = domainService.queryDomain(req);
        return resp;
	}

	@Override
	public PageRespDto<DomainDto> queryDomainPageList(@RequestBody QueryDomainPageListReq req) {
		PageRespDto<DomainDto> resp = domainService.queryDomainPageList(req);
        return resp;
	}
}
